// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CLOUDAPPENGINE_CAESIGNUTIL_H
#define CLOUDAPPENGINE_CAESIGNUTIL_H

#include "CaeRequestParams.h"
#include <cstdio>
#include "../libs/openssl/include/openssl/pem.h"

class CaeSignUtil {
public:
    CaeSignUtil();

    ~CaeSignUtil();

    bool InitSha256withRSASigner();

    bool CreateSignature(CaeRequestParams *request, time_t validDateTime);

    const std::string GetStringToSign(std::string body, std::string validDateTime);

    static bool CAESha256(const std::string& tmpVerifyStr, std::string &encodedStr);

    bool SetPriKey();

    bool Sha256RsaSign(const std::string& msg, std::string &signedMsg);

    static bool
    VerifySignature(const std::string &certStr, const std::string &data,
                    const std::string &signature);

private:
    EC_KEY *m_privateKey = nullptr;

    static void Clear(uint8_t *encStr, EVP_MD_CTX *rsaSignCtx, EVP_PKEY *priKey);

};

#endif // CLOUDAPPENGINE_CAESIGNUTIL_H
